package org.example.springservermodule;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.example.springservermodule.Bean.PersonBean;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class PersonService {
    @Resource
    private PersonMapper personMapper;

    public void insert(PersonBean personBean) {
        log.info("人员保持,检查完整度");
        if (personBean == null) {
            throw new RuntimeException("参数不能为空");
        }
        if (personBean.getAge() <= 0) {
            throw new RuntimeException("年龄不能为空");
        }
        if (personBean.getName() == null || personBean.getName().equals("")) {
            throw new RuntimeException("名字不能为空");
        }
        if (personBean.getAddress() == null || personBean.getAddress().equals("")) {
            throw new RuntimeException("地址不能为空");
        }
        if (personBean.getGender() == null || personBean.getGender().equals("")) {
            throw new RuntimeException("性别不能为空");
        }
        log.info("人员保持,检查完整度完成,执行sql语句");
        int result = personMapper.insert(personBean);
        if (result != 1) {
            throw new RuntimeException("插入失败");
        } else {
            System.out.println("插入成功");
        }
    }
    public void update(PersonBean personBean) {
        log.info("人员保持,检查完整度");
        if (personBean == null) {
            throw new RuntimeException("参数不能为空");
        }
        if (personBean.getId() <= 0) {
            throw new RuntimeException("id不能为空");
        }
        if (personBean.getAge() <= 0) {
            throw new RuntimeException("年龄不能为空");
        }
        if (personBean.getName() == null || personBean.getName().equals("")) {
            throw new RuntimeException("名字不能为空");
        }
        if (personBean.getAddress() == null || personBean.getAddress().equals("")) {
            throw new RuntimeException("地址不能为空");
        }
        if (personBean.getGender() == null || personBean.getGender().equals("")) {
            throw new RuntimeException("性别不能为空");
        }
        log.info("人员保持,检查完整度完成,执行sql语句");
        int result = personMapper.update(personBean);
        if (result != 1) {
            throw new RuntimeException("更新失败");
        } else {
            System.out.println("更新成功");
        }
    }

    public Object selectById(int id) {
        log.info("人员保持,检查完整度");
        if(id<=0){
            throw new RuntimeException("id不能为空");
        }
        try {
            return personMapper.selectById(id);
        } catch (Exception e) {
            throw new RuntimeException("查询失败",e);
        }
        }
    }


